package com.example.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.blog.constant.Constant;
import com.example.blog.data.BlogInfo;
import com.example.blog.data.UpblogPram;
import com.example.blog.mapper.BlogInfoMapper;
import com.example.blog.pojo.response.BlogInfoResponse;
import com.example.blog.pojo.resquest.AddBlogResqust;
import com.example.blog.service.BlogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

import static com.example.blog.constant.Constant.IS_DATELEFLAG;

@Slf4j
@Service("BlogService")
public class BlogServiceImpl implements BlogService {
    @Autowired
    private BlogInfoMapper blogInfoMapper;

    public List<BlogInfoResponse> getlist(){
        List<BlogInfo> blogInfos=blogInfoMapper.selectList(
                new LambdaQueryWrapper<BlogInfo>().eq(BlogInfo::getDeleteFlag, Constant.NO_DATELEFLAG)
                        .orderByAsc(BlogInfo::getId));
        //转换数据格式

        List<BlogInfoResponse> blogInfoResponses = blogInfos.stream().map(blogInfo -> {
            BlogInfoResponse response = new BlogInfoResponse();
            BeanUtils.copyProperties(blogInfo, response);
            return response;
        }).collect(Collectors.toList());
        return blogInfoResponses;
    }

    public BlogInfoResponse getBlogDetail(Integer blogId) {
        BlogInfo blogInfo=blogInfoMapper.selectById(blogId);
        BlogInfoResponse response = new BlogInfoResponse();
        BeanUtils.copyProperties(blogInfo, response);
        return response;
    }

    @Override
    public boolean addBlog(AddBlogResqust addBlogResqust) {
        Integer sum= null;
        try {
            sum = add(addBlogResqust);
            if(sum==1){
                return true;
            }
        } catch (Exception e) {
           log.info("新增文章失败:",e);
        }
        return false;
    }

    @Override
    public Boolean updateBlog(UpblogPram upblogPram) {
        BlogInfo blogInfo=new BlogInfo();
        BeanUtils.copyProperties(upblogPram,blogInfo);
        try {
            Integer sum = this.update(blogInfo);
            return sum==1;
        } catch (Exception e) {
            log.info("更新失败:",e);
        }
        return false;
    }

    @Override
    public Boolean deteleBlog(Integer blogId) {
        BlogInfo blogInfo=new BlogInfo();
        blogInfo.setId(blogId);
        blogInfo.setDeleteFlag(IS_DATELEFLAG);
        try {
            Integer sum = update(blogInfo);
            return sum==1;
        } catch (Exception e) {
            log.info("删除失败:",e);
        }
        return false;
    }

    public Integer update(BlogInfo blogInfo){
        return blogInfoMapper.updateById(blogInfo);
    }
    public Integer add(AddBlogResqust addBlogResqust){
        BlogInfo blogInfo=new BlogInfo();
        BeanUtils.copyProperties(addBlogResqust,blogInfo);
     return blogInfoMapper.insert(blogInfo);
    }
}
